/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
 * @description 测试断点管理类所有外部API
 * @jest-environment jsdom
 */

import { BreakpointManager } from '../src';

describe('BreakpointManager', () => {
  let breakpointManager: BreakpointManager;
  let callback: jest.Mock;
  let verticalCallback: jest.Mock;

  beforeEach(() => {
    callback = jest.fn();
    verticalCallback = jest.fn();
    breakpointManager = new BreakpointManager();
  });

  afterEach(() => {
    breakpointManager.destroy();
  });

  it('should call the callback with the current breakpoint when subscribed', () => {
    global.innerWidth = 400;
    global.innerHeight = 100;
    window.dispatchEvent(new Event('resize'));
    breakpointManager.subscribeToBreakpoint(callback, verticalCallback);
    expect(callback).toHaveBeenCalledWith('sm');
    expect(verticalCallback).toHaveBeenCalledWith('sm');
  });

  it('should update the current breakpoint when the window is resized', () => {
    global.innerWidth = 700;
    window.dispatchEvent(new Event('resize'));
    expect(breakpointManager.getCurrentBreakpoint()).toBe('md');
  });

  it('should update the current vertical breakpoint when the window is resized', () => {
    global.innerWidth = 700;
    window.dispatchEvent(new Event('resize'));
    expect(breakpointManager.getCurrentVerticalBreakpoint()).toBe('sm');
  });

  it('should notify all callbacks when the breakpoint changes', () => {
    breakpointManager.subscribeToBreakpoint(callback, verticalCallback);
    global.innerHeight = 700;
    window.dispatchEvent(new Event('resize'));
    expect(callback).toHaveBeenCalledWith('md');
    expect(verticalCallback).toHaveBeenCalledWith('md');
  });

  it('should use the breakpoint value for the current breakpoint', () => {
    const values = { sm: 10, md: 20, lg: 30, xl: 40 };
    expect(breakpointManager.useBreakpointValue(values)).toBe(20);
  });

  it('should use the breakpoint value for the current vertical breakpoint', () => {
    const values = { sm: 10, md: 20, lg: 30, xl: 40 };
    expect(breakpointManager.useVerticalBreakpointValue(values)).toBe(20);
  });

  it('should return the current breakpoints', () => {
    expect(breakpointManager.getBreakpoints()).toEqual({
      xs: 0,
      sm: 320,
      md: 600,
      lg: 840,
      xl: 1440,
    });
  });

  it('should return the current custom breakpoints', () => {
    let customBreakpoints = {
      xs: 0,
      sm: 300,
      md: 600,
      lg: 840,
      xl: 1440,
      xxl: 1800,
    };
    breakpointManager = new BreakpointManager(customBreakpoints);
    expect(breakpointManager.getBreakpoints()).toEqual(customBreakpoints);
  });

  it('should remove the callback when unsubscribe', () => {
    let callCount = 0;
    const callback = () => {
      callCount++;
    };
    let subscribe = breakpointManager.subscribeToBreakpoint(callback);
    subscribe();
    global.innerWidth = 1000;
    window.dispatchEvent(new Event('resize'));
    expect(callCount).toBe(1);
  });

  it('should remove the callback when destroy', () => {
    let callCount = 0;
    const callback = () => {
      callCount++;
    };
    breakpointManager.subscribeToBreakpoint(callback);
    breakpointManager.destroy();
    global.innerWidth = 1500;
    window.dispatchEvent(new Event('resize'));
    expect(callCount).toBe(1);
  });
});